package com.buka.user.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.buka.server.pojo.UserControl;
import com.buka.user.mapper.UserMapper;
import com.buka.user.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itbuka.feign.MinioFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    private MinioFeign minioFeign;
    @Override
    public List<UserControl> findAll() {

        Example example=new Example(UserControl.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete",0);
        return userMapper.selectByExample(example);

    }

    @Override
    public Page<UserControl> findByPage(Integer Page, Integer Size) {
        PageHelper.startPage(Page,Size);
        Example example=new Example(UserControl.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete",0);
        return (Page<UserControl>) userMapper.selectByExample(example);
    }

    @Override
    public int OpenOrStopBrand(Long id) {
        UserControl userControl = userMapper.selectByPrimaryKey(id);

        userControl.setStatus(userControl.getStatus() == 0 ? 1 : 0); // 修改状态值
        Example example = new Example(UserControl.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        userMapper.updateByExampleSelective(userControl, example); // 执行更新操作
        int flag =userControl.getStatus();

        return flag;
    }


    @Override
    public int update(UserControl userControl) {
        return userMapper.updateByPrimaryKeySelective(userControl);
    }

    @Override
    public int addAndFile(UserControl userControl, MultipartFile file) {

        String url = minioFeign.uploadF(file).getData();
        if (url.equals("1") || url.equals("2")) {
            return -1;
        }
        userControl.setId(IdWorker.getId());
        userControl.setAvatar(url);
        int i=userMapper.insert(userControl);
        return i;
    }

    @Override
    public int delete(Long id) {
        Example example=new Example(UserControl.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        UserControl userControl=new UserControl();
        userControl.setIsDelete(1);
        return userMapper.updateByExampleSelective(userControl,example);
    }

    @Override
    public int add(UserControl userControl) {
        userControl.setId(IdWorker.getId());
        Integer i = null;
        try {
            String hashpw = BCrypt.hashpw(userControl.getPassword(), BCrypt.gensalt());
            userControl.setPassword(hashpw);
            i = userMapper.insertSelective(userControl);
        } catch (Exception e) {
            return -2;
        }
        return i;
    }


        @Override
    public int Search() {
        return 0;
    }

    @Override
    public int login(UserControl userControl) {
//        UserControl userControl1=new UserControl();
//        userControl1.setUsername(userControl.getUsername());
//        userControl1.setStatus(0);
//        userControl1.setIsDelete(0);
//        UserControl userControl2=userMapper.selectOne(userControl1);

        Example example=new Example(UserControl.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete",0);
        criteria.andEqualTo("username",userControl.getUsername());
        UserControl userControl2 = userMapper.selectOneByExample(example);
        if (userControl2==null){
            return -1;
        } else if (userControl2.getStatus() == 0) {
            return -3;
        } else {
            return BCrypt.checkpw(userControl.getPassword(),userControl2.getPassword())?1:0;
        }
    }

    @Override
    public List<UserControl> findList(UserControl userControl) {
        Example example=new Example(UserControl.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",userControl.getUsername());
        return userMapper.selectByExample(example);
    }
}
